package com.example.jdcompetition.Service.cvsPro.dataprocess;



import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import com.example.jdcompetition.Service.cvsPro.items.Collect;
import com.example.jdcompetition.Service.cvsPro.items.Deliver;
import com.example.jdcompetition.Service.cvsPro.items.Transit;
import com.example.jdcompetition.Service.cvsPro.time.CollectTime;
import com.example.jdcompetition.Service.cvsPro.time.DeliverTime;
import com.example.jdcompetition.Service.cvsPro.time.TransitTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.example.jdcompetition.JdcompetitionApplication.*;


/**
 * @author henrydai
 * @date 2021/6/4 19:27
 */
@Service
@Slf4j
public class CsvProcess {
//    static int yy = 100000000;
//    static int ybw = 1000000;
//    static int yw = 10000;
//    static int yb = 100;
    public final static long oneSecond = 1000;
    public final static long oneMinute = 60*oneSecond;
    public final static long oneHour = 60*oneMinute;
    public final static long oneDay = 24*oneHour;

    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
//    //                      省份-->省份码
//    public static HashMap<String,String> proCode = new HashMap<>();

//    //                      省份码-->(城市名:城市码)
//    public static HashMap<String,HashMap<String,String>> proToCity;

//    //                      城市码-->(县名:县码)
//    public static HashMap<String,HashMap<String,String>> cityToDistrict;

//    //                      县码-->(镇名:镇码)
//    public static HashMap<String,HashMap<String,String>> districtToVillage;
    //*******************************处理揽收文件***********************
    public void solveCollect(Path path, String charset) throws Exception {

        //0始发省份	1始发省份编码	2始发城市	3始发城市编码
        //4始发县/区	5始发县/区编码	6始发乡/街道	7始发乡/街道编码
        //8目的省份	9目的省份编码	10目的城市	11目的城市编码
        //12揽收截止时间	13揽收时长	14生效日期	15失效日期
            BufferedReader br = Files.newBufferedReader(path, Charset.forName(charset));
            String s = br.readLine();//读第一行标题
            s=br.readLine();
            while(s!=null)
            {
                String[] ls = s.split(",");
                //始发地址
                String originateAddress = ls[1]+ls[3]+ls[5]+ls[7];
                //目的地址
                String destinationAddress = ls[9]+ls[11];


                //根据目的地址找时间
                    //处理揽收截至时间
                String[] stemp = ls[12].split(":");
                        //HHmm
                long collectDeadline = Integer.parseInt(stemp[0])*oneHour+Integer.parseInt(stemp[1])*oneMinute;
                    //处理揽收时长:
                long collectTime = Integer.parseInt(ls[13])*oneHour;
                    //处理生效时间
                long effectiveDate = solveDate(ls[14]);
                    //处理时效时间
                long expirationDate = solveDate(ls[15]);

                //添加本条数据
                Collect c = new Collect(originateAddress,destinationAddress,new CollectTime(collectDeadline,collectTime,effectiveDate,expirationDate));
                //通过起始地址查询 目的地址到揽收时间的Map
                HashMap<String,LinkedList<CollectTime>> map2 = collect.get(c.originateAddress);
                if(map2==null)
                {
                    collect.put(c.originateAddress,new HashMap<String,LinkedList<CollectTime>>());
                }
                //通过目的地址查揽收时间
                map2 = collect.get(c.originateAddress);
                LinkedList<CollectTime> list = map2.get(c.destinationAddress);
                if(list==null)
                {
                    map2.put(c.destinationAddress,new LinkedList<CollectTime>());
                }
                list = map2.get(c.destinationAddress);
                list.add(c.collectTime);
                log.debug("处理一条揽收:"+c.toString());
                s=br.readLine();
            }


    }
    //*******************************处理揽收文件***********************

    //*******************************处理中转文件***********************
    public void solveTransit(Path path, String charset) throws Exception {
        //0始发省份	1始发省份编码	2始发城市	3始发城市编码
        //4目的省份	5目的省份编码	6目的城市	7目的城市编码
        //8目的县乡  9目的县编码   10目的乡镇    11 目的乡镇码
        //12中转时长	13生效日期	14失效日期

            BufferedReader br = Files.newBufferedReader(path, Charset.forName(charset));
            String s = br.readLine();//读第一行标题
            s=br.readLine();
            while(s!=null)
            {
                String[] ls = s.split(",");
                //始发地址
                String originateAddress = ls[1]+ls[3];
                //目的地址
                String destinationAddress = ls[5]+ls[7]+ls[9]+ls[11];


                //中转时间
                String[] stt = ls[12].split("D");
                long transitTime = Integer.parseInt(stt[0])*oneDay+Integer.parseInt(stt[1])/100*oneHour+Integer.parseInt(stt[1])%100*oneMinute;
                //处理生效时间
                long effectiveDate = solveDate(ls[13]);
                //处理时效时间
                long expirationDate = solveDate(ls[14]);

                //添加本条数据
                TransitTime tt = new TransitTime(transitTime,effectiveDate,expirationDate);
                Transit t = new Transit(originateAddress,destinationAddress,tt);

                //通过起始地址查询 目的地址到揽收时间的Map
                HashMap<String,LinkedList<TransitTime>> map2 = transit.get(t.originateAddress);

                if(map2==null)
                {
                    transit.put(t.originateAddress,new HashMap<String,LinkedList<TransitTime>>());
                }
                //通过目的地址查揽收时间
                map2 = transit.get(t.originateAddress);
                LinkedList<TransitTime> list = map2.get(t.destinationAddress);
                if(list==null)
                {
                    map2.put(t.destinationAddress,new LinkedList<TransitTime>());
                }
                list = map2.get(t.destinationAddress);
                list.add(t.transitTime);
                log.debug("处理一条中转:"+t.toString());
                s=br.readLine();
            }


    }
    //*******************************处理中转文件***********************


    //*******************************处理派送文件***********************
    public void solveDeliver(Path path, String charset) throws Exception {
        //0目的省份	1目的省份编码	2目的城市	3目的城市编码
        //4目的县乡  5目的县编码   6目的乡镇    7目的乡镇码
        //8派送时长	9生效日期	10失效日期

            BufferedReader br = Files.newBufferedReader(path, Charset.forName(charset));
            String s = br.readLine();//读第一行标题
            s=br.readLine();
            while(s!=null)
            {
                String[] ls = s.split(",");
                //目的地址
                String destinationAddress = ls[1]+ls[3]+ls[5]+ls[7];



                //派送时间
                long deliverTime = Integer.parseInt(ls[8])*oneHour;
                long effectiveDate = solveDate(ls[9]);
                //处理时效时间
                long expirationDate = solveDate(ls[10]);
                DeliverTime dt = new DeliverTime(deliverTime,effectiveDate,expirationDate);
                //添加本条数据
                Deliver d = new Deliver(destinationAddress,dt);

                //通过起始地址查询 目的地址到揽收时间的Map
                LinkedList<DeliverTime> list = deliver.get(destinationAddress);
                if(list==null)
                {
                    deliver.put(destinationAddress,new LinkedList<DeliverTime>());
                }
                list = deliver.get(destinationAddress);
                list.add(dt);

                log.debug("处理一条派送:"+d.toString());
                s=br.readLine();
            }

    }
    //*******************************处理派送文件***********************



    long solveDate(String sDate) throws ParseException {
        return sdf.parse(sDate).getTime();
    }
}
